<template>
  <div>
    <div style="background-color: #f4f5f9; padding: 20px;">
      <el-form inline>
        <el-form-item label="话题">
          <el-select style="width: 100px" v-model="filters.topicId" placeholder="选择类型">
            <el-option
              v-for="(item, index) in topics" :key="index"
              :label="item.title" :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="发帖时间">
          <el-date-picker
            v-model="filters.timeRange"
            type="datetimerange"
            range-separator="到"
            start-placeholder="起始"
            end-placeholder="截至"
            style="width: 300px"
            class="date-picker"
          />
        </el-form-item>
        <el-form-item label="点赞量">
          <div class="input-number-wrapper">
            <el-input-number :controls="false" class="input-number" v-model="filters.startLikeNum" :step="1" />
            <span>-</span>
            <el-input-number :controls="false" class="input-number" v-model="filters.endLikeNum" :step="1" />
          </div>
        </el-form-item>
        <el-form-item label="收藏量">
          <div class="input-number-wrapper">
            <el-input-number :controls="false" class="input-number" v-model="filters.startCollectNum" :step="1" />
            <span>-</span>
            <el-input-number :controls="false" class="input-number" v-model="filters.endCollectNum" :step="1" />
          </div>
        </el-form-item>
        <el-form-item label="浏览量">
          <div class="input-number-wrapper">
            <el-input-number :controls="false" class="input-number" v-model="filters.startViewNum" :step="1" />
            <span>-</span>
            <el-input-number :controls="false" class="input-number" v-model="filters.endViewNum" :step="1" />
          </div>
        </el-form-item>
        <el-form-item label="字数">
          <div class="input-number-wrapper">
            <el-input-number :controls="false" class="input-number" v-model="filters.startWordNum" :step="1" />
            <span>-</span>
            <el-input-number :controls="false" class="input-number" v-model="filters.endWordNum" :step="1" />
          </div>
        </el-form-item>
        <el-form-item label="是否匿名">
          <el-select style="width: 80px" v-model="filters.isNameless" placeholder="选择">
            <el-option :value="null" label="不限"></el-option>
            <el-option :value="true" label="是"></el-option>
            <el-option :value="false" label="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否隐藏">
          <el-select style="width: 80px" v-model="filters.isDie" placeholder="选择">
            <el-option :value="null" label="不限"></el-option>
            <el-option :value="true" label="是"></el-option>
            <el-option :value="false" label="否"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-input v-model="filters.contentKey" placeholder="搜索内容" style="width: 200px;"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getPosts">筛选</el-button>
        </el-form-item>
      </el-form>
    </div>


    <div v-if="posts.length" class="post-list">
      <div class="post-card" v-for="post in posts" :key="post.id">
        <h3 class="post-title">{{post.contentText}}</h3>

        <h3 class="post-topic">话题：{{ post.topic }}</h3>
        <p class="post-time">发布于 {{ post.createTime }}</p>
        <div class="post-stats">
          <span>点赞量: {{ post.likeNum }}</span>
          <span>收藏量: {{ post.collectNum }}</span>
          <span>浏览量: {{ post.viewNum }}</span>
          <span>字数: {{ post.wordNum }}</span>
        </div>
        <div style="display: flex;margin-top: 20px;justify-content: space-between">
          <div class="post-tags">
            <el-tag :type="post.isNameless ? 'success' : 'info'">匿名: {{ post.isNameless ? '是' : '否' }}</el-tag>
            <el-tag :type="post.isDie ? 'danger' : 'info'">隐藏: {{ post.isDie ? '是' : '否' }}</el-tag>
          </div>
          <div class="post-actions">
            <el-button >编辑</el-button>
            <el-button >数据</el-button>
            <el-button >查看评论</el-button>
          </div>
        </div>
      </div>
    </div>



    <el-empty description="暂无数据" v-else></el-empty>

    <el-pagination
      style="margin-top: 20px; text-align: right;"
      :current-page="pagination.pageNum"
      :page-size="pagination.pageSize"
      :total="total"
      :page-sizes="[5, 10, 15, 20]"
      @current-change="(value) => pagination.pageNum = value"
      @size-change="(value) => pagination.pageSize = value"
      layout="jumper, ->, sizes, total, prev, pager, next"
    ></el-pagination>

  </div>
</template>

<script setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { getPostTopics, pagePostsByFilters } from '@/api/post/postManagement.js'

const filters = reactive({
  topicId: null,
  startTime: null,
  endTime: null,
  startLikeNum: null,
  endLikeNum: null,
  startCollectNum: null,
  endCollectNum: null,
  startViewNum: null,
  endViewNum: null,
  startWordNum: null,
  endWordNum: null,
  contentKey: '',
  isNameless: null,
  timeRange: null,
  isDie: null
})

const pagination = reactive({
  pageNum: 1,
  pageSize: 10
})

const posts = ref([])
const topics = ref([])
const total = ref(0)

const getPosts = () => {
  pagePostsByFilters({
    ...filters,
    ...pagination
  }).then((res) => {
    posts.value = res.data.records
    total.value = res.data.total


    posts.value.forEach((item) => {
      item.topic = topics.value.find(record => record.id == item.topicId).title
    })
  })
}


onMounted(() => {
  getPostTopics().then((res) => {
    topics.value = res.data
    getPosts()
  })  
})
</script>

<style scoped>

.input-number-wrapper {
  display: flex;
  gap: 3px;
  font-weight: bold;
}
.input-number {
  width: 60px;
}
.input-number :deep(.el-input__wrapper) {
  border-radius: 8px!important;
}
:deep(.date-picker .el-range-input) {
  font-size: 10px!important;
}

.post-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.post-card {
  background-color: #ffffff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  width: 98%;
  flex-direction: column;
  justify-content: space-between;
}
.post-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 10px;
  white-space: pre-wrap;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  word-break: break-all;

}
.post-time {
  font-size: 12px;
  color: #888;
  margin-bottom: 10px;
  margin-left: auto;
}
.post-topic {
  font-size: 14px;
  margin-left: auto;
}
.post-stats {
  font-size: 12px;
  color: #666;
  margin-bottom: 10px;
  display: flex;
  gap: 5px;
}
.post-tags {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}
.post-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

</style>

